<?php

namespace api\modules\v1\controllers\clinic;

use common\models\clinic\Project;
use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use common\models\clinic\TreatmentRoom;
use api\modules\v1\forms\clinic\TreatmentRoomForm;

/**
 * Class TreatmentRoomController
 * @package api\modules\v1\controllers\clinic
 */
class TreatmentRoomController extends OnAuthController
{
    /**
     * @var TreatmentRoom
     */
    public $modelClass = TreatmentRoom::class;

    /**
     * @api {get} /v1/clinic/treatment-room/list 治疗室列表
     * @apiName list
     * @apiGroup Clinic
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   治疗室列表
     * @apiSuccess {Number}  data.list.id                治疗室ID
     * @apiSuccess {Number}  data.list.project_id        项目ID
     * @apiSuccess {String}  data.list.name              治疗室名称
     * @apiSuccess {Array}   data.list.projetc_names_arr 治疗项目数组
     * @apiSuccess {Number}  data.list.status            状态
     * @apiSuccess {String}  data.list.status_str        状态值
     * @apiSuccess {String}  data.list.icon_id           图标ID
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 0;
        $pageSize = $params['pageSize'] ?? 0;

        // 查询结果
        $data = Yii::$app->services->clinicTreatmentRoom->getListPage($params, (int)$page, (int)$pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->clinicTreatmentRoom->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/clinic/treatment-room/info 治疗室详情
     * @apiName info
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                                            治疗室ID
     *
     * @apiSuccess {Object}     data                                    返回数据
     * @apiSuccess {Number}     data.id                                 治疗室ID
     * @apiSuccess {String}     data.name                               治疗室名称
     * @apiSuccess {Number}     data.status                             状态
     * @apiSuccess {Number}     data.icon_id                            图标ID
     * @apiSuccess {Array}      data.clinic_project_id_Arr              绑定治疗项目数组
     * @apiSuccess {Array}      data.clinic_project_list                治疗项目列表
     * @apiSuccess {Number}     data.clinic_project_list.id             治疗项目ID
     * @apiSuccess {String}     data.clinic_project_list.name           治疗项目名称
     * @apiSuccess {Number}     data.clinic_project_list.status         治疗项目状态
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        // 诊室信息
        $clinicTreatmentRoom = Yii::$app->services->clinicTreatmentRoom->findById($id);

        // 绑定列表
        $clinicProjectBind = Yii::$app->services->clinicProjectBind->findList(['treatment_room_id' => $id]);
        $clinicProjectIds = array_column($clinicProjectBind, 'clinic_project_id');

        // 治疗项目列表
        $clinicProject = Yii::$app->services->clinicProject->findList([
            'project_id' => $this->projectId,
            'status' => [Project::STATUS_INVALID, Project::STATUS_ENABLE],
        ]);
        $clinicProjectFormat = Yii::$app->services->clinicProject->findListFormat($clinicProject);

        return [
            'id' => $clinicTreatmentRoom->id ?? 0,
            'name' => $clinicTreatmentRoom->name ?? '',
            'status' => $clinicTreatmentRoom->status ?? TreatmentRoom::STATUS_ENABLE,
            'icon_id' => $clinicTreatmentRoom->icon_id ?? 1,
            'clinic_project_id_arr' => $clinicProjectIds,
            'clinic_project_list' => $clinicProjectFormat,
        ];
    }

    /**
     * @api {post} /v1/clinic/treatment-room/create 新增治疗室
     * @apiName create
     * @apiGroup Clinic
     *
     * @apiParam {String} name                           治疗室名称
     * @apiParam {Number} status                         状态【1启用、0停用】
     * @apiParam {Number} icon_id                        图标ID【1-12】
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗室ID
     */
    public function actionCreateTreatmentRoom()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new TreatmentRoomForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicTreatmentRoom->create($model);
    }

    /**
     * @api {post} /v1/clinic/treatment-room/update 编辑治疗室
     * @apiName update
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             治疗室ID
     * @apiParam {String} name                           治疗室名称
     * @apiParam {Number} status                         状态【1启用、0停用】
     * @apiParam {Number} icon_id                        图标ID【1-12】
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗室ID
     */
    public function actionUpdateTreatmentRoom()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new TreatmentRoomForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicTreatmentRoom->edit($model);
    }

    /**
     * @api {post} /v1/clinic/treatment-room/delete 删除治疗室
     * @apiName delete
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             治疗室ID
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗室ID
     */
    public function actionDeleteTreatmentRoom()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new TreatmentRoomForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicTreatmentRoom->delete($model);
    }

    /**
     * @api {post} /v1/clinic/treatment-room/update-status 编辑治疗室状态
     * @apiName update-status
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             治疗室ID
     * @apiParam {Number} status                         状态【1启用、0停用】
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗室ID
     */
    public function actionUpdateTreatmentRoomStatus()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new TreatmentRoomForm();
        $model->setScenario('edit_status');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicTreatmentRoom->editStatus($model);
    }

    /**
     * @api {post} /v1/clinic/treatment-room/bind-project 绑定治疗项目【治疗室】
     * @apiName bind-project
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             治疗室ID
     * @apiParam {String} clinic_project_ids             治疗项目IDs【多个用','分割】
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     治疗室ID
     */
    public function actionBindProject()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new TreatmentRoomForm();
        $model->setScenario('bind_project');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicTreatmentRoom->bindProject($model);
    }
}
